<?php

namespace App\Libs\Company\SheetTemplateHandler\Report;

use App\Libs\Company\SheetTemplateHandler\Helper;
use http\Exception\BadQueryStringException;

class Template108 extends AbstractTemplate
{
    public $rules = [
        'column' => [
            'start_row' => 3,
            'end_row' => ['str' => '填报说明', 'column' => 0],
            'rule' => [
                ['column' => 0, 'vt' => 2],
                ['column' => 1, 'vt' => 2],
                ['column' => 2, 'vt' => 1],
                ['column' => 3, 'vt' => 2],
                ['column' => 4, 'vt' => 1],
                ['column' => 5, 'vt' => 1],
                ['column' => 6, 'vt' => 1],
                ['column' => 7, 'vt' => 2],
                ['column' => 8, 'vt' => 2],
                ['column' => 9, 'vt' => 1],
                ['column' => 10, 'vt' => 1],
                ['column' => 11, 'vt' => 1],
                ['column' => 12, 'vt' => 1],
            ],
        ],
        'cell' => [

        ],
        'dict' => [

        ],
    ];

    public function generateReport()
    {
        $this->renderHeader();
        $this->output['sheets']['sheet-01']['cellData']['0']['0'] = $this->createTitleCell('日喀则市' . $this->getDistrictName($this->subTask->district_id) . "{$this->subTask->task->year}年{$this->subTask->task->key}月\r{$this->template->name}");
        return $this->output;
    }

    public function selfValidate($data, $submit) {
        if (!$submit) {
            return [];
        }

        $errData = [];
        $cellData = $data['sheets']['sheet-01']['cellData'];

        //直接就业人数加上间接就业人数等于农牧民参与乡村旅游人数
        $val = $cellData[3][5]['v']+ $cellData[3][6]['v'];
        if (bccomp($val, $cellData[3][4]['v'], 2) != 0) {
            $errMsg = '直接就业人数加上间接就业人数应等于农牧民参与乡村旅游人数';
            $errData[] = ['row' => 3, 'column' => 4, 'msg' => "E4{$errMsg}"];
        }

        //乡村旅游就业人数大于或等于农牧民参与多村旅游就业人数
        if (bccomp($cellData[3][2]['v'], $cellData[3][4]['v'], 2) == -1) {
            $errMsg = '乡村旅游就业人数应大于或等于农牧民参与乡村旅游就业人数';
            $errData[] = ['row' => 3, 'column' => 2, 'msg' => "C4{$errMsg}"];
        }

        //衣牧民参与乡村旅游总收入除以农牧民参与旅游人数等于乡村旅游人均收入
        if (!empty($cellData[3][4]['v'])) { //排除除数是0
            $val = $cellData[3][7]['v'] / $cellData[3][4]['v'];
            $sub = abs(bcsub($val,$cellData[3][8]['v'],2));
            //0.4667 0.46和0.47都支持
            if ($sub >= 0.01) {
                $errMsg = '农牧民参与乡村旅游总收入除以农牧民参与旅游人数应等于乡村旅游人均收入';
                $errData[] = ['row' => 3, 'column' => 8, 'msg' => "I4{$errMsg}"];
            }

//            if (bccomp($val, $cellData[3][8]['v'], 2) != 0) {
//                $errMsg = '农牧民参与乡村旅游总收入除以农牧民参与旅游人数应等于乡村旅游人均收入';
//                $errData[] = ['row' => 3, 'column' => 8, 'msg' => "I4{$errMsg}"];
//            }
        }

        return $errData;
    }
}
